package com.example.musiccommunity.UserPage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.example.musiccommunity.MainActivity;
import com.example.musiccommunity.R;
import com.example.musiccommunity.bean.Record;
import com.example.musiccommunity.bean.Song;
import com.example.musiccommunity.util.GridAdapter;
import com.example.musiccommunity.util.MusicDataLoader;

import java.io.File;
import java.util.ArrayList;

public class MyDownload extends AppCompatActivity {

    private GridView grid;
    BaseAdapter mAdapter = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_download);
        grid = findViewById(R.id.grid);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ArrayList<Song> arrayList=new ArrayList<>();
        File internalDir = getApplicationContext().getFilesDir();
        for (Song s: MainActivity.songs
             ) {
            File file = new File(internalDir, s.getRawResId()+".mp3");
            if (file.exists()) {
                Log.d("FileCheck",  s.getRawResId()+".mp3"+"文件存在");
                arrayList.add(s);
            }
        }

        mAdapter = new GridAdapter<Song>(arrayList, R.layout.list_song) {
            @Override
            public void bindView(ViewHolder holder, Song obj) {
                holder.setText(R.id.name, obj.getName());
                holder.setText(R.id.artist, obj.getArtist());
                int coverResId = MusicDataLoader.getDrawableResourceId(getBaseContext(), obj.getCoverResId());
                holder.setImageBgResource2(R.id.img,coverResId);
            }

        };
        grid.setAdapter(mAdapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                System.out.println(arrayList.get(i).getName());
                intent.putExtra("name",arrayList.get(i).getRawResId());
                intent.setClass(getBaseContext() , PlayLocalMusic.class);
                startActivity(intent);
            }
        });

    }
}